package com.tanhua.sso.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.sso.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;
@Service
@Slf4j
public class
UserInfoService {
    @Autowired
   private UserService userService;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private FaceEngineService faceEngineService;
    public Result saveUserInfo(Map<String, String> parm, String token) {
        User user = userService.findUserbytoken(token);
        if(user==null){
            return Result.builder().code("0007").msg("您的token已经过期了").build();
        }
        UserInfo userInfo =new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setSex(StringUtils.equalsIgnoreCase(parm.get("gender"),"man")? SexEnum.MAN:SexEnum.WOMAN);
        userInfo.setNickName(parm.get("nickname"));
        userInfo.setBirthday(parm.get("birthday"));
        userInfo.setCity(parm.get("city"));
        userInfoMapper.insert(userInfo);
        return null;
    }

    public Result saveUserlogo(MultipartFile file, String token) {
        User user = userService.findUserbytoken(token);
        if(user==null){
            return Result.builder().code("0007").msg("您的token已经过期了").build();
        }
        try {
            boolean b = faceEngineService.checkIsPortrait(file.getBytes());
            if(!b){
                return Result.builder().code("0008").msg("人脸识别失败").build() ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*PicUploadResult result = picUploadService.upload(file);*/
        PicUploadResult results = new PicUploadResult();
        results.setName("http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/1.jpg");
        if(StringUtils.isEmpty(results.getName())){
            return Result.builder().code("0009").msg("文件上传失败").build();
        }UserInfo userInfo = new UserInfo();
         userInfo.setLogo(results.getName());
        QueryWrapper<UserInfo> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",user.getId());
        userInfoMapper.update(userInfo,wrapper);
        return null;
    }
}